package com.qiwx.pigeon.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.qiwx.pigeon.R;

/**
 * @description
 * @Author qiwx
 * @Date 2020-04-02 09:50
 **/
public class EggView extends View {
    Paint paint;
    Paint txtPaint;
    int eggColor;
    int width = 30;//宽度
    int height = 60;//高度
    int txtColor = 60;//字体颜色
    int txtSize = 60;//字体大小
    String content = "";

    public EggView(Context context) {
        this(context, null);
    }

    public EggView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EggView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 获取自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.EggView);
        if (typedArray != null) {
            width = (int) typedArray.getDimension(R.styleable.EggView_width, 0);
            height = (int) typedArray.getDimension(R.styleable.EggView_height, 0);
            eggColor = typedArray.getColor(R.styleable.EggView_eggColor, 0);
            txtColor = typedArray.getColor(R.styleable.EggView_txtColor, context.getResources().getColor(R.color.arrow_bg));
            txtSize = (int) typedArray.getDimension(R.styleable.EggView_txtSize, 0);
            content = typedArray.getString(R.styleable.EggView_eggName);
        }
        initView();
    }

    private void initView() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(1);
        txtPaint = new Paint();
        txtPaint.setAntiAlias(true);
        txtPaint.setStyle(Paint.Style.FILL);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(eggColor);
        int startX = getWidth() / 2;
        int startY = getHeight() / 2;
        RectF rectF = new RectF(startX - width / 2, startY - height / 2, startX + width / 2, startY + height / 2);
        canvas.drawOval(rectF, paint);
        //写文字
        txtPaint.setTextSize(txtSize);
        txtPaint.setColor(txtColor);

        if (content != null && content.length() > 0) {
            int fontwidth = content.length() * txtSize;
            int fontheight = txtSize;
            canvas.drawText(content, startX - 10, startY + 10, txtPaint);
        }
    }

    public void setEggColor(int color) {
        eggColor = color;
        invalidate();
    }

    public void setText(String content) {
        this.content = content;
        invalidate();
    }
}
